/**
 * 用户(授权者)Authorizer Model
 * @authors 杨兴洲（of2502）
 * @date    2017/3/3 9:49
 * @version 1.0
 */

import {model, Schema} from "mongoose";

const UserSchema = new Schema({
    userID: {type:String,required:true},
    authorizer_appid: {type:String,required:true},
    authorizer_refresh_token: String,
    adNewsUsedList: {
        "type": Array,
        "default": []
    },
    ticket:String,
    ticketExpireAt:Date
}, {timestamps: true,});

UserSchema.index({userID: 1}, {unique: true});

UserSchema.virtual('jsapiTicket').get(function() {
    let {ticket,ticketExpireAt} = this;
    ticketExpireAt = ticketExpireAt ? ticketExpireAt : new Date();
    if (new Date() > ticketExpireAt || !ticket){
        ticket = ''
    }
    return ticket;
});
UserSchema.virtual('jsapiTicketExpireAt').get(function() {
    return this.ticketExpireAt;
});
//确保查询结果以json返回时包含虚拟字段
UserSchema.set('toJSON', { getters: true, virtual: true });


export default model('user', UserSchema);
